//
package views;


import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import main.Main;
import bean.*;


public class PanelDocumentos extends javax.swing.JPanel {
   
    private static int idDoc;
    
    public int getId(){
        return idDoc;
    }
    public void setId(int id){
        idDoc=id;
    }
    
    DocumentoTableModel docTM= new DocumentoTableModel();
    ContenedorTableModel contTM= new ContenedorTableModel();
    
    class ContenedorTableModel extends AbstractTableModel{
        
        ArrayList<Contenedor> conts = main.Main.service.queryAllContxDoc(idDoc);
        String titulos[]={"CODIGO","DESCRIPCION MERCADERIA","TIPOCONTENEDOR"};
        
        
        @Override
        public int getRowCount() {
            return conts.size();
        }

        @Override
        public int getColumnCount() {
            return 3 ;
        }

        @Override
       public Object getValueAt(int rowIndex, int columnIndex) {
            String res=null;
            Contenedor cont= conts.get(rowIndex);
                
             switch(columnIndex){
                case 0: res = "" + cont.getId();break;
                case 1:res="" + cont.getDescripcionMerc();break;
                case 2:res=Float.toString(Main.service.TotalFac(cont.getId()));
             }


            return res;
        }
        
        @Override
        public String getColumnName(int col){//COLOCA LOS TITULOS DESEADOS
            return titulos[col];
        }
        
    }
    
    class DocumentoTableModel extends AbstractTableModel{
        
        ArrayList<Documento> docs= Main.service.queryAllDocumento();
        String titulos[]={"IdDocumento","IdCliente","Total"};
        @Override
        public int getRowCount() {
            return docs.size();
        }

        @Override
        public int getColumnCount() {
            return 3 ;
        }

        @Override
        public Object getValueAt(int fil, int col) {
            String cad=null;
            Documento doc=docs.get(fil);
            switch (col){
                
                case 0:cad=Integer.toString(doc.getId());break;
                case 1:cad=Integer.toString(doc.getIdCliente());break;
               case 2:cad=Float.toString(Main.service.TotalFac(doc.getId()));
                       //ouble.toString(doc.getTotal());
            }
            return cad;
        }
        
        @Override
        public String getColumnName(int col){
            return titulos[col];
        }
    }
    
    public void Actualizar(){
        docTM.docs=Main.service.queryAllDocumento();
        docTM.fireTableChanged(null);
    }
          
      
    public void ActualizaTCont(){
        contTM.conts=Main.service.queryAllContxDoc(idDoc);
        contTM.fireTableChanged(null);
    }
    
      
    public PanelDocumentos() {
        initComponents();
        TablaDoc.setModel(docTM);
        TablaCont.setModel(contTM);
        Actualizar();
        ActualizaTCont();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaDoc = new javax.swing.JTable();
        btnAdd1 = new javax.swing.JButton();
        btnClean1 = new javax.swing.JButton();
        btnAddCont = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        TablaCont = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        txtDoc = new javax.swing.JTextField();
        txtPuertoO = new javax.swing.JTextField();
        txtPuertoE = new javax.swing.JTextField();
        txtIdCliente = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtReg = new javax.swing.JTextField();
        txtNave = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jButtonDelete = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jLabel7.setText("Lista de Documentos Tramarsa");

        TablaDoc.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "IdDocumento", "IdCliente", "Total"
            }
        ));
        TablaDoc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TablaDocMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TablaDoc);

        btnAdd1.setText("Agregar");
        btnAdd1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdd1ActionPerformed(evt);
            }
        });

        btnClean1.setText("Limpiar");
        btnClean1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClean1ActionPerformed(evt);
            }
        });

        btnAddCont.setText("Añadir Contenedor");
        btnAddCont.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddContActionPerformed(evt);
            }
        });

        TablaCont.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(TablaCont);

        jLabel6.setText("Contenedores del Cliente");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Datos del Documento"));

        jLabel8.setText("Id:");

        jLabel1.setText("Tipo de Documento:");

        jLabel2.setText("Puerto de Origen:");

        jLabel3.setText("Puerto de Embarque:");

        jLabel4.setText("IdCliente:");

        txtId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIdActionPerformed(evt);
            }
        });

        txtIdCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIdClienteActionPerformed(evt);
            }
        });

        jLabel10.setText("Regimen Comercia:l");

        jLabel11.setText("Nave:");

        txtReg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtRegActionPerformed(evt);
            }
        });

        txtNave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNaveActionPerformed(evt);
            }
        });

        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/views/logo.gif"))); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel8)
                    .addComponent(jLabel4)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPuertoO, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDoc, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPuertoE, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtIdCliente)
                        .addComponent(txtReg)
                        .addComponent(txtNave, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel13)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtDoc, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtPuertoO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtPuertoE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtIdCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(txtReg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(txtNave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(231, 231, 231))))
        );

        jButtonDelete.setText("Eliminar");
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteActionPerformed(evt);
            }
        });

        jButton1.setText("<< Añadir a Factura");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAdd1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnClean1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonDelete))
                            .addComponent(btnAddCont, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd1)
                    .addComponent(btnClean1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonDelete))
                .addGap(18, 18, 18)
                .addComponent(btnAddCont)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(jButton1)))
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAdd1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdd1ActionPerformed

        Documento d = new Documento();
        d.setId(Integer.parseInt(txtId.getText()));
        d.setIdCliente(Integer.parseInt(txtIdCliente.getText()));
        d.setPuertoOrigen(txtPuertoO.getText());
        d.setPuertoEmbarque(txtPuertoE.getText());
        d.setTipoDoc(txtDoc.getText());
        d.setRegc(txtReg.getText());
        d.setNave(txtNave.getText());
               
        Main.service.agregaDocumento(d);
        Actualizar(); 
    }//GEN-LAST:event_btnAdd1ActionPerformed

    private void btnAddContActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddContActionPerformed
              
        InternalFrameContenedor1 cont=Main.principal.cont;
        Main.principal.getEscritorio().add(cont);
        cont.toFront();
        cont.setVisible(true);
    }//GEN-LAST:event_btnAddContActionPerformed

    private void btnClean1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClean1ActionPerformed
        txtId.setText("");
        txtIdCliente.setText("");
        txtPuertoE.setText("");
        txtPuertoO.setText("");
        txtDoc.setText("");
        txtIdCliente.setText("");
        txtReg.setText("");
        txtNave.setText("");
    }//GEN-LAST:event_btnClean1ActionPerformed

    private void txtIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIdActionPerformed

    private void txtIdClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIdClienteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIdClienteActionPerformed

    private void txtRegActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtRegActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtRegActionPerformed

    private void txtNaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNaveActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNaveActionPerformed

    private void TablaDocMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaDocMouseClicked
        int rowSel=TablaDoc.getSelectedRow();
        String idDocu=TablaDoc.getValueAt(rowSel,0).toString();
        idDoc=Integer.parseInt(idDocu);
        ActualizaTCont();        
    }//GEN-LAST:event_TablaDocMouseClicked

    private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteActionPerformed
        int rowSel=TablaDoc.getSelectedRow();
        String idDocu=TablaDoc.getValueAt(rowSel,0).toString();
        Main.service.eliminaDocumento(idDocu);
        Actualizar();
    }//GEN-LAST:event_jButtonDeleteActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int rowSel=TablaDoc.getSelectedRow();
        int iddoc= Integer.parseInt(TablaDoc.getValueAt(rowSel, 0).toString());
        String cliente= TablaDoc.getValueAt(rowSel, 1).toString(); 
       float total=Main.service.TotalFac(iddoc);
        
        Main.principal.doc.setVisible(false);
        main.Main.principal.fact.getPanelFactura1().setTxtTotal(total);
        main.Main.principal.fact.getPanelFactura1().setTxtDoc(iddoc);
        main.Main.principal.fact.getPanelFactura1().setTxtCliente(cliente);
        
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable TablaCont;
    private javax.swing.JTable TablaDoc;
    private javax.swing.JButton btnAdd1;
    private javax.swing.JButton btnAddCont;
    private javax.swing.JButton btnClean1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField txtDoc;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtIdCliente;
    private javax.swing.JTextField txtNave;
    private javax.swing.JTextField txtPuertoE;
    private javax.swing.JTextField txtPuertoO;
    private javax.swing.JTextField txtReg;
    // End of variables declaration//GEN-END:variables
}
